{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Error Handling and Exceptions "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-24-0106664d39e8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m5\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "5/0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "It is properly running\n",
      "It is a code which will run no matter what\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    5/1\n",
    "    print(\"It is properly running\")\n",
    "except:\n",
    "    print(\"It was an error thats where we are in Except block\")\n",
    "finally:\n",
    "    print(\"It is a code which will run no matter what\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Database not found or its down try loging into Insta after some time\n",
      "division by zero\n",
      "Close the connection, from the DB\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    5/0\n",
    "    print(\"Get the user from the database\")\n",
    "    print(\"Database found\")\n",
    "except Exception as e:\n",
    "    print(\"Database not found or its down try loging into Insta after some time\")\n",
    "    print(e)\n",
    "finally:\n",
    "    print(\"Close the connection, from the DB\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The file gave us error -  not writable\n",
      "ABC\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    file = open(\"FCS.txt\",'r')\n",
    "    file.write(\"ABC\")\n",
    "except Exception as e:\n",
    "    print(\"The file gave us error - \", e)\n",
    "    file = open(\"FCS.txt\",'r')\n",
    "    print(file.read())\n",
    "finally:\n",
    "    file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We dont have any option of our program closing down at any point \n",
    "\n",
    "# But i am little bit worried about the line of code and think that\n",
    "# this may or may not give error \n",
    "\n",
    "# then what to do ?\n",
    "\n",
    "# we use Try, Except and Finally "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Try except and finally blocks gives us \n",
    "# as layer to try differnt code, \n",
    "\n",
    "# if at all ur try block code explodes or breaks \n",
    "\n",
    "# you go and handle that in the Except block with a error message to user, \n",
    "#not to complier\n",
    "\n",
    "# Finally block is somehting which makes sure ur \n",
    "# code is closing or ending properly\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unit Testing \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# You make a large programs, \n",
    "\n",
    "# you want progarm to work properly, with atmost performance \n",
    "\n",
    "# you wANT THEm to work as designed nothing else \n",
    "\n",
    "\n",
    "# how to go about it ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pylint \n",
    "\n",
    "# Unittest "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pylint in /opt/anaconda3/lib/python3.7/site-packages (2.4.2)\n",
      "Requirement already satisfied: mccabe<0.7,>=0.6 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (0.6.1)\n",
      "Requirement already satisfied: astroid<2.4,>=2.3.0 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (2.3.1)\n",
      "Requirement already satisfied: isort<5,>=4.2.5 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (4.3.21)\n",
      "Requirement already satisfied: six==1.12 in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.12.0)\n",
      "Requirement already satisfied: typed-ast<1.5,>=1.4.0; implementation_name == \"cpython\" and python_version < \"3.8\" in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.1)\n",
      "Requirement already satisfied: wrapt==1.11.* in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.11.2)\n",
      "Requirement already satisfied: lazy-object-proxy==1.4.* in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.2)\n",
      "\u001b[33mWARNING: You are using pip version 20.0.2; however, version 20.1 is available.\n",
      "You should consider upgrading via the '/opt/anaconda3/bin/python -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install pylint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ourPyLitExampleFile.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile ourPyLitExampleFile.py\n",
    "a = 1\n",
    "b = 2\n",
    "if a == b:\n",
    "    print(\"Both are equal\")\n",
    "else:\n",
    "    print(\"Both are UnEqual\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "************* Module ourPyLitExampleFile\r\n",
      "ourPyLitExampleFile.py:1:0: C0103: Module name \"ourPyLitExampleFile\" doesn't conform to snake_case naming style (invalid-name)\r\n",
      "ourPyLitExampleFile.py:1:0: C0114: Missing module docstring (missing-module-docstring)\r\n",
      "ourPyLitExampleFile.py:1:0: C0103: Constant name \"a\" doesn't conform to UPPER_CASE naming style (invalid-name)\r\n",
      "ourPyLitExampleFile.py:2:0: C0103: Constant name \"b\" doesn't conform to UPPER_CASE naming style (invalid-name)\r\n",
      "\r\n",
      "------------------------------------------------------------------\r\n",
      "Your code has been rated at 2.00/10 (previous run: 6.00/10, -4.00)\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "! pylint ourPyLitExampleFile.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting our_file.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile our_file.py\n",
    "'''\n",
    "A Sample Program for testing PyLint\n",
    "'''\n",
    "A = 1\n",
    "B = 2\n",
    "if A == B:\n",
    "    print(\"Both are equal\")\n",
    "else:\n",
    "    print(\"Both are UnEqual\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "--------------------------------------------------------------------\r\n",
      "Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "! pylint our_file.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unittest Library "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "#u want to add multiple numbers\n",
    "\n",
    "#if you are sending 2 numbers it should add them both, \n",
    "\n",
    "#if u are sending multiple numbers its should run a for loop and get he answer\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "def addNumbers(*args):\n",
    "    '''\n",
    "    1. send two int parameter as for there addition \n",
    "    2. Senf a list only with ints to add them all \n",
    "    '''\n",
    "    if len(args) == 2:\n",
    "        return args[0] + args[1]\n",
    "    elif len(args)>2:\n",
    "        return \"Refer Doc String\"\n",
    "    else:\n",
    "        addNum = 0 \n",
    "        for item in args[0]:\n",
    "            addNum = addNum + item\n",
    "        return addNum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "addNumbers(1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11850"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "addNumbers([1000,4545,23,6112,24,63,4,32,23,24])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Refer Doc String'"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "addNumbers(1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting adding_Multiplenumbers.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile adding_Multiplenumbers.py\n",
    "def addNumbers(*args):\n",
    "    '''\n",
    "    1. send two int parameter as for there addition\n",
    "    2. Senf a list only with ints to add them all\n",
    "    '''\n",
    "    if len(args) == 2:\n",
    "        return args[0] + args[1]\n",
    "    elif len(args) > 2:\n",
    "        return \"Refer to doc string\"\n",
    "    else:\n",
    "        addNum = 0\n",
    "        for item in args[0]:\n",
    "            addNum = addNum + item\n",
    "        return addNum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "************* Module adding_Multiplenumbers\r\n",
      "adding_Multiplenumbers.py:1:0: C0103: Module name \"adding_Multiplenumbers\" doesn't conform to snake_case naming style (invalid-name)\r\n",
      "adding_Multiplenumbers.py:1:0: C0114: Missing module docstring (missing-module-docstring)\r\n",
      "adding_Multiplenumbers.py:1:0: C0103: Function name \"addNumbers\" doesn't conform to snake_case naming style (invalid-name)\r\n",
      "adding_Multiplenumbers.py:6:4: R1705: Unnecessary \"elif\" after \"return\" (no-else-return)\r\n",
      "adding_Multiplenumbers.py:11:8: C0103: Variable name \"addNum\" doesn't conform to snake_case naming style (invalid-name)\r\n",
      "adding_Multiplenumbers.py:13:12: C0103: Variable name \"addNum\" doesn't conform to snake_case naming style (invalid-name)\r\n",
      "\r\n",
      "------------------------------------------------------------------\r\n",
      "Your code has been rated at 3.33/10 (previous run: 1.11/10, +2.22)\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "! pylint adding_Multiplenumbers.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting unittestof_Twonumbers.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile unittestof_Twonumbers.py\n",
    "import unittest\n",
    "import adding_Multiplenumbers\n",
    "\n",
    "class TestAddition(unittest.TestCase):\n",
    "    \n",
    "    def test_two_numbers(self):\n",
    "        A = 1\n",
    "        B = 2\n",
    "        result = adding_Multiplenumbers.addNumbers(A,B)\n",
    "        self.assertEqual(result,3)\n",
    "        \n",
    "    def test_multiple_numbers(self):\n",
    "        list_of_Numbers = list(range(1,200))\n",
    "        result = adding_Multiplenumbers.addNumbers(list_of_Numbers)\n",
    "        self.assertEqual(result,19900)\n",
    "    \n",
    "    def test_three_args(self):\n",
    "        A= 1\n",
    "        B= 2\n",
    "        C= 3\n",
    "        result = adding_Multiplenumbers.addNumbers(A,B,C)\n",
    "        self.assertEqual(result,\"Refer to doc string\")\n",
    "        \n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    unittest.main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "...\r\n",
      "----------------------------------------------------------------------\r\n",
      "Ran 3 tests in 0.000s\r\n",
      "\r\n",
      "OK\r\n"
     ]
    }
   ],
   "source": [
    "! python unittestof_Twonumbers.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "#if __name__ == '__main__':\n",
    "#    unittest.main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
